package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    public EmployeeService employeeService;
  @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
      String username = employee.getUsername();
      String password = employee.getPassword();
      password = DigestUtils.md5DigestAsHex(password.getBytes());
      QueryWrapper<Employee>queryWrapper=new QueryWrapper<>();
      queryWrapper.eq("username",username);
      Employee emp = employeeService.getOne(queryWrapper);
      if (emp==null){
          return R.error("用户名不准确");
      }
      if (!emp.getPassword().equals(password)){
            return R.error("密码不准确");
      }
      if (emp.getStatus()==0){
          return R.error("员工账户已禁用");
      }
      //6、登录成功，将员工id存入Session并返回登录成功结果
      request.getSession().setAttribute("employee", emp.getId());
      return R.success(emp);
  }
  @PostMapping("logout")
    public R<String>logout(HttpSession session){
     //让服务器的session失效
      session.invalidate();
      return R.success("成功下线");
  }
  @PostMapping
  public R<String>save(@RequestBody Employee employee,HttpServletRequest request){
    String password = DigestUtils.md5DigestAsHex("123456".getBytes());
    employee.setPassword(password);
    //时间
//    employee.setCreateTime(LocalDateTime.now());
//    employee.setUpdateTime(LocalDateTime.now());
//    Long id = (Long) request.getSession().getAttribute("employee");
//    employee.setUpdateUser(id);
//    employee.setCreateUser(id);
    employeeService.save(employee);
    return R.success("添加成功");
  }
  @GetMapping("/page")
  public R<Page<Employee>>page(Integer page,Integer pageSize,String name){
    Page<Employee> pageInfo = new Page<>(page, pageSize);
    QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
    queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
    queryWrapper.orderByDesc("update_time");
    employeeService.page(pageInfo,queryWrapper);
    return R.success(pageInfo);

  }
  @GetMapping("/{id}")
  public R<Employee>getById(@PathVariable Long id){
    Employee byId = employeeService.getById(id);
    return R.success(byId);
  }
  @PutMapping
  public R<String>update(@RequestBody Employee employee,HttpServletRequest request){
    Long empId = (Long) request.getSession().getAttribute("employee");
    employee.setUpdateUser(empId);
    employee.setUpdateTime(LocalDateTime.now());
    employeeService.updateById(employee);
    return R.success("修改成功");
  }
}
